using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityX;

namespace UnityXEditor
{
    internal static class EditorUGUITool
    {
        [MenuItem("GameObject/Layout/转换为UGUI自适应")]
        public static void ToUGUIAutoSize()
        {
            foreach (GameObject go in Selection.gameObjects)
            {
                if (go.GetComponent<LayoutBase>() == null)
                {
                    go.GetComponent<RectTransform>().ExSizeToAnchor();
                }
                else if (go.GetComponent<LayoutRectTransform>() != null)
                {
                    go.GetComponent<RectTransform>().ExSizeToAnchor();
                    UnityEngine.Object.DestroyImmediate(go.GetComponent<LayoutRectTransform>());
                }
            }
        }
        [MenuItem("GameObject/Layout/转换为UGUI自适应(包括子节点)")]
        public static void ToUGUIAutoSizeAll()
        {
            if (!EditorUtility.DisplayDialog("转换为UGUI自适应", "如果已经添加UGUI布局组件可能产生冲突！\n比如：\nScrollRect\nGridLayoutGroup\nHorizontalLayoutGroup\nVerticalLayoutGroup\n请谨慎使用此功能，是否继续？", "确定", "取消"))
            {
                return;
            }
            foreach (GameObject go in Selection.gameObjects)
            {
                foreach (RectTransform t in go.GetComponentsInChildren<RectTransform>(true))
                {
                    if (t.gameObject.GetComponent<LayoutBase>() == null)
                    {
                        t.gameObject.GetComponent<RectTransform>().ExSizeToAnchor();
                    }
                    else if (t.gameObject.GetComponent<LayoutRectTransform>() != null)
                    {
                        t.gameObject.GetComponent<RectTransform>().ExSizeToAnchor();
                        UnityEngine.Object.DestroyImmediate(t.gameObject.GetComponent<LayoutRectTransform>());
                    }
                }
            }
        }

        [MenuItem("GameObject/Layout/转换为UnityX自适应")]
        public static void ToUnityXAutoSize()
        {
            foreach (GameObject go in Selection.gameObjects)
            {
                if (go.GetComponent<LayoutBase>() == null)
                {
                    go.AddComponent<LayoutRectTransform>();
                }
            }
        }
        [MenuItem("GameObject/Layout/转换为UnityX自适应(包括子节点)")]
        public static void ToUnityXAutoSizeAll()
        {
            if (!EditorUtility.DisplayDialog("转换为UnityX自适应", "如果已经添加UGUI布局组件可能产生冲突！\n比如：\nScrollRect\nGridLayoutGroup\nHorizontalLayoutGroup\nVerticalLayoutGroup\n请谨慎使用此功能，是否继续？", "确定", "取消"))
            {
                return;
            }
            foreach (GameObject go in Selection.gameObjects)
            {
                foreach (RectTransform t in go.GetComponentsInChildren<RectTransform>(true))
                {
                    if (t.gameObject.GetComponent<LayoutBase>() == null)
                    {
                        t.gameObject.AddComponent<LayoutRectTransform>();
                    }
                }
            }
        }

        [MenuItem("GameObject/Layout/转换为高清Text")]
        public static void ToHDText()
        {
            foreach (GameObject gameObject in Selection.gameObjects)
            {
                foreach (LayoutText text in gameObject.GetComponentsInChildren<LayoutText>(true))
                {
                    LayoutRectTransformOnlySize layoutRectTransformOnlySize = text.gameObject.GetComponent<LayoutRectTransformOnlySize>();
                    if (layoutRectTransformOnlySize == null)
                    {
                        RectTransform rectTransform = text.transform as RectTransform;
                        RectTransform parentRectTransform = text.transform.parent as RectTransform;
                        float ws = (float)Math.Round(rectTransform.rect.width / parentRectTransform.rect.width * 2, 4);
                        float hs = (float)Math.Round(rectTransform.rect.height / parentRectTransform.rect.height * 2, 4);
                        rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
                        rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
                        layoutRectTransformOnlySize = text.gameObject.AddComponent<LayoutRectTransformOnlySize>();
                        layoutRectTransformOnlySize.Set(ws, LayoutRectTransformOnlySize.WidthSizeType.基于父亲宽度, hs, LayoutRectTransformOnlySize.HeightSizeType.基于父亲高度);
                        text.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
                    }
                }
            }
        }

        [MenuItem("GameObject/Layout/转换为超高清Text")]
        public static void ToQHDText()
        {
            foreach (GameObject gameObject in Selection.gameObjects)
            {
                foreach (LayoutText text in gameObject.GetComponentsInChildren<LayoutText>(true))
                {
                    LayoutRectTransformOnlySize layoutRectTransformOnlySize = text.gameObject.GetComponent<LayoutRectTransformOnlySize>();
                    if (layoutRectTransformOnlySize == null)
                    {
                        RectTransform rectTransform = text.transform as RectTransform;
                        RectTransform parentRectTransform = text.transform.parent as RectTransform;
                        float ws = (float)Math.Round(rectTransform.rect.width / parentRectTransform.rect.width * 5, 4);
                        float hs = (float)Math.Round(rectTransform.rect.height / parentRectTransform.rect.height * 5, 4);
                        rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
                        rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
                        layoutRectTransformOnlySize = text.gameObject.AddComponent<LayoutRectTransformOnlySize>();
                        layoutRectTransformOnlySize.Set(ws, LayoutRectTransformOnlySize.WidthSizeType.基于父亲宽度, hs, LayoutRectTransformOnlySize.HeightSizeType.基于父亲高度);
                        text.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
                    }
                }
            }
        }

        [MenuItem("GameObject/Layout/转换为超高清Text", true)]
        [MenuItem("GameObject/Layout/转换为高清Text", true)]
        [MenuItem("GameObject/Layout/转换为UGUI自适应", true)]
        [MenuItem("GameObject/Layout/转换为UGUI自适应(包括子节点)", true)]
        [MenuItem("GameObject/Layout/转换为UnityX自适应", true)]
        [MenuItem("GameObject/Layout/转换为UnityX自适应(包括子节点)", true)]
        public static bool MenuItemTest()
        {
            foreach (GameObject go in Selection.gameObjects)
            {
                if (go.GetComponent<RectTransform>() != null)
                {
                    return true;
                }
            }
            return false;
        }
    }
}